package com.demo.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author zhu
 * @date 2021/1/21 13:43
 */
public class FileUpUtils {

    /**
     * 上传文件
     *
     * @param file 文件
     * @param path 绝对路径
     * @return flag
     */
    public static Boolean upFile(MultipartFile file, String path) {

        //获取上传文件名,包含后缀
        String originalFilename = file.getOriginalFilename();
        //获取后缀
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //保存的文件名 or 避免文件重复名称
        // String dFileName = UUID.randomUUID()+substring;
        //保存路径 服务器文件路径
//        String path = "C:\\FileTest\\";
        //生成保存文件
        File uploadFile = new File(path + originalFilename);
        //将上传文件保存到路径
        try {
            file.transferTo(uploadFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 获取下载文件
     *
     * @param response 返回
     * @param filesUrl      url地址
     * @return 浏览器文件下载
     * @throws UnsupportedEncodingException 异常
     */
    public static String downLoad(HttpServletResponse response, String filesUrl) throws UnsupportedEncodingException {
        String filename = FileUpUtils.getFilename(filesUrl);
        File file = new File(filesUrl);
        //判断文件父目录是否存在
        if (file.exists()) {
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(filename, "UTF-8"));
            byte[] buffer = new byte[1024];
            //文件输入流
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            //输出流
            OutputStream os = null;
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer);
                    i = bis.read(buffer);
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----------file download---" + filename);
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 获取Url路径中文件名
     *
     * @param url
     * @return
     */
    public static String getFilename(String url) {
        File tempFile = new File(url.trim());
        String fileName = tempFile.getName();
        return fileName;

    }

    /**
     * 获取文件大小 转换为  M
     *
     * @param file 文件
     * @return M
     */
    public static Double fileSize(MultipartFile file) {
        long size = file.getSize();
        Double fileSize = size * 0.00000095367432;
        return fileSize;
    }

    /**
     * 获取文件服务器绝对路径
     * 路径参数拼接
     *
     * @param file 文件
     * @param path 服务器路径
     * @return 路径
     */
    public static String getLocalPath(MultipartFile file, String path) {
        String originalFilename = file.getOriginalFilename();
        String localPath = path + originalFilename;
        return localPath;
    }
}




